package interfaces.admin;

import exceptions.FatalDebugException;
import exceptions.FatalException;
import exceptions.Util;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * A class implementing admin's port listener.<br>
 * <br>
 *
 * @author Mihail A. Muhin
 */
//TODO: interruption after errors
public class PortListener extends Thread {

	//port to listen to
	//TODO: read it from settings object
	private final static int port = 5001;

	public void run() {
		try {
			internal_run();
		} catch (FatalException e) {
			Util.ShowFatalError(e.getMessage());
		} catch (FatalDebugException e) {
			Util.ShowFatalError(e.getMessage());
//    } catch (CorrectableException e){
//      Util.LogError(e.getMessage());
//    } catch (CorrectableDebugException e){
//      Util.LogError(e.getMessage());
		} catch (RuntimeException e) {
			Util.ShowFatalError(e.getMessage());
		}
	}

	private void internal_run() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(port);
		} catch (Exception e) {
			throw new FatalException("ServerSocket creation failed");
		}

		try {
			Socket cs = ss.accept();
		} catch (IOException e) {
			Util.LogError("Admin connection failed");
		}
	}
}
